﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserControl_ControlKhoHang : System.Web.UI.UserControl
{
    #region global variables
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";
    #endregion


    #region protected methods
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (ViewState[DRMConstants.STOREPROC_PARAM_FLAG_IS_NEW] == null)
                {
                    ViewState[DRMConstants.STOREPROC_PARAM_FLAG_IS_NEW] = false;
                }
         
            DRMCommonUtils.LoadTTPPDropDowList(ddlTTPP, getLogURL(),Session["sitecode"].ToString());
        }
    }

    protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        ViewState[DRMConstants.STOREPROC_PARAM_FLAG_IS_NEW] = true;
        clearDetail();
        txtKho.Focus();
    }

    protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
       
        try
        {
            if ((bool)ViewState[DRMConstants.STOREPROC_PARAM_FLAG_IS_NEW])
            {
                //insert
                insertKhoHang();
                ViewState[DRMConstants.STOREPROC_PARAM_FLAG_IS_NEW] = false;
                clearDetail();
            }
            else
            {
                //update
                updateKhoHang();
            }
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }

    protected void imgBtnFind_Click(object sender, ImageClickEventArgs e)
    {
        TabContainer1.ActiveTabIndex = 1;
    }

    protected void grvKhoHang_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == DRMConstants.COMMAND_NAME_ROW_COMMAND)
        {
            GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;

            Label lblMaSo = (Label)grv.FindControl(DRMConstants.CONTROL_NAME_LABEL_MASO);
            DataRow detail = getDetailKhoHang(lblMaSo.Text.Trim());
            fillDetail(detail);

            ViewState[DRMConstants.STOREPROC_PARAM_FLAG_IS_NEW] = false;

            grvKhoHang.DataSource = null;
            grvKhoHang.DataBind();

            TabContainer1.ActiveTabIndex = 0;
        }
    }

    protected void grvKhoHang_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvKhoHang.PageIndex = e.NewPageIndex;
        loadGrid();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        loadGrid();
    }
    #endregion


    #region private methods
    private void clearDetail()
    {
        txtMaSo.Text = DRMConstants.EMPTY_STRING;
        txtKho.Text = DRMConstants.EMPTY_STRING;
        ddlTTPP.SelectedIndex = 0;
        chbxIsImport.Checked = false;
    }

    private void fillDetail(DataRow detailKhoHang)
    {
        txtKho.Text = detailKhoHang[DRMConstants.DB_COLUMN_LOCATION_DESC].ToString();
        txtMaSo.Text = detailKhoHang[DRMConstants.DB_COLUMN_LOCATION_ID].ToString();
        
        ddlTTPP.SelectedValue = detailKhoHang[DRMConstants.DB_COLUMN_TTPP].ToString();
        if (detailKhoHang[DRMConstants.DB_COLUMN_LOCATION_DEFAULT] != null &&
            (bool)detailKhoHang[DRMConstants.DB_COLUMN_LOCATION_DEFAULT])
        {
            chbxIsImport.Checked = true;
        }
        else
        {
            chbxIsImport.Checked = false;
        }
    }

    private DataRow getDetailKhoHang(string locationID)
    {
        dt = new ClassExecute.ClassExecute();

        DataTable db = dt.Get_Data_DataTable(str, ref error, DRMConstants.STOREPROC_NAME_GET_DETAIL_MAST, CommandType.StoredProcedure,
                        new SqlParameter(DRMConstants.STOREPROC_PARAM_LOCATION_ID, locationID));

        return db.Rows[0];
    }

    private void insertKhoHang()
    {
        int result;        
        result = dt.ExecuteSQl_int(str, DRMConstants.STOREPROC_NAME_SAVE_LOCATION, CommandType.StoredProcedure, ref error,
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_LOCATION_ID, txtMaSo.Text.Trim()),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_DESC, txtKho.Text.Trim()),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_TTPP, ddlTTPP.SelectedValue.ToString()),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_FLAG_IS_NEW, true),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_FLAG_IS_DEFAULT, chbxIsImport.Checked));
        if (result == 0)
        {
            uscMsgBox1.AddMessage(String.Format(DRMConstants.MESSAGE_ADD_SUCCESSFULL, DRMConstants.OBJECT_NAME_LOCATION), uscMsgBox.enmMessageType.Success);
        }
        else
        {
            uscMsgBox1.AddMessage(DRMConstants.MESSAGE_UNDER_CONSTRUCTION, uscMsgBox.enmMessageType.Error);
        }
    }

    private void updateKhoHang()
    {       
        if (txtMaSo.Text.Trim().Length > 0)
        {
            dt = new ClassExecute.ClassExecute();

            int result = 0;
            result = dt.ExecuteSQl_int(str, DRMConstants.STOREPROC_NAME_SAVE_LOCATION, CommandType.StoredProcedure, ref error,
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_LOCATION_ID, txtMaSo.Text.Trim()),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_DESC, txtKho.Text.Trim()),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_TTPP, ddlTTPP.SelectedValue.ToString()),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_FLAG_IS_NEW, false),
                    new SqlParameter(DRMConstants.STOREPROC_PARAM_FLAG_IS_DEFAULT, chbxIsImport.Checked));
            if (result == 1)
            {
                uscMsgBox1.AddMessage(String.Format(DRMConstants.MESSAGE_UPDATE_SUCCESSFULL, DRMConstants.OBJECT_NAME_LOCATION),
                    uscMsgBox.enmMessageType.Success);
            }
            else
            {
                uscMsgBox1.AddMessage(DRMConstants.MESSAGE_UNDER_CONSTRUCTION, uscMsgBox.enmMessageType.Error);
            }
        }
        else
        {
            uscMsgBox1.AddMessage(String.Format(DRMConstants.MESSAGE_NOT_SELECTED, DRMConstants.OBJECT_NAME_LOCATION),
                    uscMsgBox.enmMessageType.Error);
        }
    }

    private void loadGrid()
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            DataTable db = dt.Get_Data_DataTable(str, ref error, DRMConstants.STOREPROC_NAME_SEARCH_LOCATIONS, CommandType.StoredProcedure,
                new SqlParameter(DRMConstants.STOREPROC_PARAM_CONTENT, txtSearch.Text.Trim()));
            grvKhoHang.DataSource = db;
            grvKhoHang.DataBind();
            if (db.Rows.Count == 0)
            {
                uscMsgBox1.AddMessage(DRMConstants.MESSAGE_NO_RECORD_FOUND, uscMsgBox.enmMessageType.Attention);
            }
        }
        catch (Exception ex)
        {
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }
    }

    private string getLogURL()
    {
        return Server.MapPath(webapp4U.Globals.LogUrl());
    }
    #endregion
}